package newxml;

import com.jme.animation.SpatialTransformer;
import com.jme.app.SimpleGame;
import com.jme.image.Texture;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.Controller;
import com.jme.scene.Node;
import com.jme.scene.Text;
import com.jme.scene.TriMesh;
import com.jme.scene.shape.Quad;
import com.jme.scene.shape.Teapot;
import com.jme.scene.state.LightState;
import com.jme.scene.state.MaterialState;
import com.jme.scene.state.TextureState;
import com.jme.util.TextureManager;
import com.jme.util.geom.BufferUtils;
import com.jmex.model.animation.KeyframeController;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

public class TestXML_InOut extends SimpleGame{
    public static void main(String[] args) {
        TestXML_InOut app = new TestXML_InOut();
        app.setDialogBehaviour(ALWAYS_SHOW_PROPS_DIALOG);
        app.start();
    }

    @Override
    protected void simpleInitGame() {
        try {
            final com.jme.scene.Node n = new com.jme.scene.Node("my <\"test\"> & &lt; node");
            SpatialTransformer st = new SpatialTransformer(1);
            st.setObject(n, 0, -1);
            st.setPosition(0, 0.0f, new Vector3f(0, 0, 0));
            st.setPosition(0, 1.0f, new Vector3f(0, 10, 0));
            st.setPosition(0, 2.0f, new Vector3f(0, 0, 0));
            st.setRepeatType(Controller.RT_WRAP);
            n.addController(st);
            n.getLocalTranslation().set(2, 1, 1);
            
            final Teapot teapot = new Teapot("teapot");
            Texture tex = TextureManager.loadTexture(
                    Thread.currentThread().getContextClassLoader().getResource(
                    "jmetest/data/texture/wall.jpg"));
            tex.setWrap(Texture.WM_WRAP_S_WRAP_T);
            TextureState ts = display.getRenderer().createTextureState();
            ts.setTexture(tex);
            teapot.setRenderState(ts);
            
            n.attachChild(teapot);

            com.jme.scene.TriMesh tm = new com.jme.scene.TriMesh("", BufferUtils.createFloatBuffer(0, 0, 0, 1, 0, 0, 1, 1, 0), BufferUtils.createFloatBuffer(0, 0, 1, 0, 0, 1, 0, 0, 1), null, null, BufferUtils.createIntBuffer(new int[]{0, 1, 2}));

            tm.setTextureBuffer(0, BufferUtils.createFloatBuffer(0, 0, 0, 1, 0, 0, 1, 1, 0), 0);
            tm.setTextureBuffer(0, BufferUtils.createFloatBuffer(0, 0, 0, 1, 0, 0, 1, 1, 0), 1);

            KeyframeController kfc = new KeyframeController();
            kfc.setMorphingMesh(tm);
            
            MaterialState ms = display.getRenderer().createMaterialState();
            
            ms.setColorMaterial(MaterialState.CM_AMBIENT_AND_DIFFUSE);
            ms.setShininess(2f);
            tm.setRenderState(ms);
            tm.setRandomColors();
            tm.setLocalTranslation(0, -2, 0);

            TriMesh tm1 = new TriMesh("tm2", tm.getVertexBuffer(0), tm.getNormalBuffer(0), tm.getColorBuffer(0), tm.getTextureBuffer(0, 0), tm.getIndexBuffer(0));
            tm1.setVertexBuffer(0, BufferUtils.createFloatBuffer(0, 0, 0, 1, 0, 0, 1, 1, 0));
            kfc.setKeyframe(0, tm1);

            TriMesh tm2 = new TriMesh("tm2", tm.getVertexBuffer(0), tm.getNormalBuffer(0), tm.getColorBuffer(0), tm.getTextureBuffer(0, 0), tm.getIndexBuffer(0));
            tm2.setVertexBuffer(0, BufferUtils.createFloatBuffer(0, 0, 0, 5, 0, 0, 1, 1, 0));
            kfc.setKeyframe(1, tm2);

            kfc.setRepeatType(Controller.RT_WRAP);
            tm.addController(kfc);

            n.attachChild(tm);

            Text text = Text.createDefaultTextLabel("text", "TestXML_InOut says hello!");
            text.setLocalTranslation(20, display.getHeight()/2, 0);
            text.setLightCombineMode(LightState.OFF);
            text.setTextColor(ColorRGBA.orange);
            n.attachChild(text);

            n.attachChild(new Node("empty node"));
            
            Quad q = new Quad("quad", 10, 30);
            q.setLightCombineMode(LightState.OFF);
            q.setRenderQueueMode(Renderer.QUEUE_ORTHO);
            q.setRandomColors();
            q.setLocalTranslation(display.getWidth()/2, display.getHeight()-32, 0);
            n.attachChild(q);
            
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            new XMLExporter().save(n, bos);
            bos.flush();
            bos.close();
            final byte[] bytes = bos.toByteArray();
            System.out.write(bytes);
            ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
//            XMLReaderFactory.createXMLReader().parse(new InputSource(bis));
            final Node n2 = (Node) new XMLImporter().load(bis);
            System.out.println(n2.getLocalTranslation());

            new XMLExporter().save(n2, System.out);
            
            rootNode.attachChild(n2);
            
//            JMETreeModel.showScenegraph(rootNode);
        } catch (Exception ex) {
            Logger.getLogger(TestXML_InOut.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
